package com.shengqin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.common.pojo.CommonResult;
import com.shengqin.config.CommonException;
import com.shengqin.entity.*;
import com.shengqin.pojo.plan.ScoreTemplateParam;
import com.shengqin.service.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/v1/template")
public class ScoreTemplateController {

    @Resource
    private IScoreTemplateService scoreTemplateService;
    @Resource
    private SysHealthProjectService projectService;
    @Resource
    private IStandardService standardService;
    @Resource
    private ITotalScoreStandardService totalScoreStandardService;
    @Resource
    private IUserService userService;


    @PostMapping("/queryTotalStandardPage")
    public CommonResult<Page<TotalScoreStandard>> queryTotalStandardPage(@RequestBody CommonPage<TotalScoreStandard> commonPage){
        return CommonResult.data(totalScoreStandardService.queryTotalStandardPage(commonPage));
    }

    @PostMapping("/saveTotalStandard")
    public CommonResult<Boolean> saveTotalStandard(@RequestBody TotalScoreStandard totalScoreStandard){
        return CommonResult.data(totalScoreStandardService.saveOrUpdate(totalScoreStandard));
    }

    @PostMapping("/deleteTotalStandard")
    public CommonResult<Boolean> deleteTotalStandard(@RequestBody TotalScoreStandard totalScoreStandard){
        return CommonResult.data(totalScoreStandardService.removeById(totalScoreStandard.getId()));
    }

    @PostMapping("/queryEditPermission")
    public CommonResult<Boolean> queryEditPermission(@RequestBody ScoreTemplate scoreTemplate){
        ScoreTemplate oldScoreTemplate = scoreTemplateService.getById(scoreTemplate.getId());
        if(oldScoreTemplate != null){
            Integer loginId = Integer.valueOf(StpUtil.getLoginId().toString());
            User user = userService.getById(loginId);
            if(user.getRoleId() != 202401 && !oldScoreTemplate.getCreateUserId().equals(loginId)){
                throw new CommonException("您无权限操作该数据！");
            }
        }
        return CommonResult.data(true);
    }

    @PostMapping("/queryPage")
    public CommonResult<Page<ScoreTemplate>> queryPage(@RequestBody CommonPage<ScoreTemplateParam> scoreTemplateParam){
        return CommonResult.data(scoreTemplateService.queryPage(scoreTemplateParam));
    }

    @PostMapping("/list")
    public CommonResult<List<ScoreTemplate>> list(@RequestBody ScoreTemplateParam scoreTemplateParam){
        return CommonResult.data(scoreTemplateService.queryList(scoreTemplateParam));
    }

    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> save(@RequestBody ScoreTemplate scoreTemplate){
        boolean isOk = scoreTemplateService.onSave(scoreTemplate);
        if(scoreTemplate.getCopyId() != null){
            List<Standard> standardList = standardService.listByTemplateId(scoreTemplate.getCopyId());
            List<SysHealthProject> projectList = projectService.listByTemplateId(scoreTemplate.getCopyId());
            standardList.forEach(standard -> {
                standard.setId(null);
                standard.setTemplateId(scoreTemplate.getId());
            });
            projectList.forEach(project -> {
                project.setId(null);
                project.setTemplateId(scoreTemplate.getId());
            });
            if(!CollectionUtils.isEmpty(standardList)){
                standardService.saveBatch(standardList);
            }
            if(!CollectionUtils.isEmpty(projectList)){
                projectService.saveBatch(projectList);
            }
        }
        return CommonResult.data(isOk);
    }

    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> delete(@RequestBody ScoreTemplate scoreTemplate){
        List<Standard> standardList = standardService.listByTemplateId(scoreTemplate.getId());
        List<SysHealthProject> projectList = projectService.listByTemplateId(scoreTemplate.getId());
        if(!CollectionUtils.isEmpty(standardList)){
            standardService.removeBatchByIds(standardList.stream().map(Standard::getId).collect(Collectors.toSet()));
        }
        if(!CollectionUtils.isEmpty(projectList)){
            projectService.removeBatchByIds(projectList.stream().map(SysHealthProject::getId).collect(Collectors.toSet()));
        }
        return CommonResult.data(scoreTemplateService.removeById(scoreTemplate.getId()));
    }

}
